<template>
	<view class="marketingBox">
		<u-parse @ready="ready" :content="content"></u-parse>
		<!-- <image mode="widthFix" src="/static/img/marketing.png"></image> -->
		<view class="marketingTast">
			<view class="marketingUnlock" @click="pdPhone">进入老谭餐饮情报局</view>
			<!-- <view class="marketingnoUnlock" @click="passPages">跳过</view> -->
		</view>

		<!--获取手机  -->
		<u-popup :show="show" round="24">
			<view class="phoneBox">
				<view class="phoneBox_herad">
					<view class="phoneBox_title">绑定手机号</view>
					<image @click="show=false" class="phoneBox_chacha" src="/static/img/chacha.png"></image>
				</view>
				<view class="phoneBox_main">
					<image class="phoneBox_icon" src="/static/img/getPhoneIcon.png"></image>
					<view class="phoneBox_text">
						为了能提供更好的服务与体验</br>请允许我们获取你的微信手机号
					</view>
					<button class="phoneBox_phone primary" style="margin-top: 64rpx;" open-type="getPhoneNumber"
						@getphonenumber="decryptPhoneNumber">一键获取手机号</button>
					<view class="phoneBox_phone" @click="toFreeVersion">进入体验版</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				show: false,
				content: ``
			}
		},
		onLoad() {
			// #ifndef H5
			let search = uni.getMenuButtonBoundingClientRect()
			this.$store.commit('updataSearch', search);
			// #endif
			this.init()
		},
		computed: {
			search() {
				return this.$store.state.search;
			},
			user() {
				return this.$store.state.user;
			},
		},
		methods: {
			ready() {
				uni.hideLoading()
			},
			getLocation() {
				let that = this;
				let location = uni.getStorageSync('location');
				let phone = uni.getStorageSync('phone');

				if ((that.user && that.user.phone) || phone) {
					let nowPhone = (that.user && that.user.phone) ? that.user.phone : phone;
					uni.setStorageSync('phone', nowPhone);
					uni.navigateTo({
						url: '/package/citySelection/index?isBlack=1'
					})
				} else {
					that.show = true;
				}


			},

			pdPhone() {
				this.passPages();
				this.getLocation();

			},
			decryptPhoneNumber(e) {

				if (!this.show) {
					return;
				}
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					this.$api.userWxGetPhone({
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv
					}).then(res => {
						if (res.code == 200) {
							let phone = res.data

							uni.showToast({
								title: '获取成功',
								icon: 'none',
								mask: true
							});
							let user = uni.getStorageSync('user');
							user = user ? user : {};
							user.phone = phone;
							uni.setStorageSync('phone', phone);
							this.$store.commit('updataUser', user);

							this.$api.accountWxEditInfo({
								phone
							}).then(res => {
								if (res.code == 200) {
									this.$store.commit('updataUser', res.data.user);

								}
							})



							uni.navigateTo({
								url: '/package/citySelection/index?isBlack=1'
							})
						}
					})

				} else {
					uni.showToast({
						icon: 'none',
						title: '获取失败',
						mask: true
					})
				}
				this.show = false;

			},
			toFreeVersion() {
				uni.navigateTo({
					url: '/package/citySelection/index?isBlack=1'
				})
			},
			init() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				this.$api.operateInfo({
					site: 'marketing_page'
				}).then(res => {
					if (res.code == 200) {

						let content = res.data.content;
						this.content = content.replace(/\<img/gi, '<img class="myStyle"');

					}
				})
			},
			passPages() {
				let myPassTime = new Date().getTime();
				uni.setStorageSync('passTime', myPassTime);

				// uni.switchTab({
				// 	url: '/pages/index/index'
				// })

			}
		}
	}
</script>

<style lang="scss" scoped>
	.marketingBox {
		width: 100%;

		/deep/.myStyle {
			display: block;
			width: 100%;
		}

		image {
			width: 100%;
		}

		.marketingTast {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;

			padding-bottom: 72rpx;
			background: linear-gradient(360deg, rgba(0, 0, 0, .55), transparent);

			view {
				width: 558rpx;
				height: 88rpx;
				line-height: 88rpx;
				text-align: center;
				font-size: 28rpx;
				font-family: 蘋方-簡;
				font-weight: normal;
				color: #000000;
				background: #FFDA2D;
				box-shadow: 0px 4px 8px rgba(255, 218, 45, 0.3);
				border-radius: 24rpx;
				margin: 32rpx auto;
			}
		}

		.phoneBox_icon {
			width: 440rpx;
			height: 112rpx;
		}

		.marketingnoUnlock {
			background-color: #fff !important;

		}

		.phoneBox {
			background-color: #fff;
			border-radius: 24rpx 24rpx 0 0;

		}

		.phoneBox_herad {
			height: 108rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1rpx solid #dfdfdf;
			padding: 0 32rpx;
			box-sizing: border-box;
		}

		.phoneBox_title {
			height: 44rpx;
			font-size: 32rpx;
			font-family: 蘋方-簡;
			font-weight: normal;
			line-height: 44rpx;
			color: #333333;
		}

		.phoneBox_chacha {
			height: 44rpx;
			width: 44rpx;
		}

		.phoneBox_main {
			display: flex;
			flex-direction: column;
			align-items: center;
			position: relative;
			padding: 64rpx;
		}

		.phoneBox_phone {
			width: 100%;
			line-height: 88rpx;
			border-radius: 24rpx;
			font-size: 24rpx;
			font-weight: bold;
			text-align: center;
			border: 1rpx solid #efefef;
			margin-top: 32rpx;

			&.primary {
				background-color: #FFDA2D;
			}
		}

		.phoneBox_text {
			font-size: 32rpx;
			line-height: 48rpx;
			color: #333333;
			text-align: center;
			margin-top: 48rpx;
		}
	}
</style>
